<template>
  <el-dialog
    :title="$t('message.ship')"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item :label="$t('message.expressName')" prop="shipCompany" :class="{ 'is-required': true }">
        <el-input v-model="dataForm.shipCompany" :placeholder="$t('message.expressName')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('message.expressNo')" prop="shipNo" :class="{ 'is-required': true }">
        <el-input v-model="dataForm.shipNo" :placeholder="$t('message.expressNo')"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{$t('message.cancle')}}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{$t('message.submit')}}</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: '',
          shipCompany: '',
          shipNo: ''
        },
        dataRule: {
          shipCompany: [
            { required: true, message: this.$t('message.noEmpty'), trigger: 'blur' }
          ],
          shipNo: [
            { required: true, message: this.$t('message.noEmpty'), trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.visible = true
        this.dataForm.id = id
        if (this.$refs['dataForm'] !== undefined) {
          this.$refs['dataForm'].resetFields()
        }
      },
      dataFormSubmit () {
        this.$http({
          url: this.$http.adornUrl(`/sys/purchaseOrder/ship`),
          method: 'post',
          data: this.$http.adornData({
            'orderId': this.dataForm.id,
            'shipCompany': this.dataForm.shipCompany,
            'shipNo': this.dataForm.shipNo
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: this.$t('message.success'),
              type: 'success',
              duration: 500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          } else {
            this.$message.error(this.$t('message.failure'))
          }
        })
      }
    }
  }
</script>
